﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Lessn13_里氏替换原则
{
    //父类容器装子类对象  
    //任何父类出现的地方，子类都可以替换
    //方便进行对象的管理和存储
    class GameObjects
    {

    }
    class Monster:GameObjects

    {
        public void  MonsterAttack()
        {
            Console.WriteLine("怪物攻击");
        }
    } class Player:GameObjects

    {
        public void PlayerAttack()
        {
            Console.WriteLine("玩家攻击");
        }
    } class Boss:GameObjects

    {
        public void  BossAttack()
        {
            Console.WriteLine("怪兽攻击");
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            GameObjects player = new Player();
            GameObjects boss = new Boss();
            GameObjects monster = new Monster();
            GameObjects[] gameobjects = new GameObjects[] { new Player(), new Monster(), new Boss() };
            //使用is和as关键字进行调用

            if (player is Player)
            {
                //Player p = player as Player;
                //p.PlayerAttack();
               
              (player as Player).PlayerAttack();    //一步到位
            }
           
            //遍历打印
            for(int i  = 0;i<gameobjects.Length;i++)
            {
                if (gameobjects[i] is Player)
                {
                    (gameobjects[i] as Player).PlayerAttack();
                }
                 else if (gameobjects[i] is Monster)
                {
                    (gameobjects[i] as Monster).MonsterAttack();
                }
                else if (gameobjects[i] is Boss)
                {
                    (gameobjects[i] as Boss).BossAttack();
                }
            }
        }
    }
}
